﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.Types.Interval;
using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a color filter
    /// It works like the rgb color filter, except
    /// the H, S, B values are filtered instead of R G B
    /// </summary>
    public class HSBFilter : BaseForeBackgroundColorFilter
    {
        protected FloatInterval m_HInterval = new FloatInterval(0.0f, 1.0f);
        protected FloatInterval m_SInterval = new FloatInterval(0.0f, 1.0f);
        protected FloatInterval m_BInterval = new FloatInterval(0.0f, 1.0f);

        /// <summary>
        /// Default constructor
        /// </summary>
        public HSBFilter()
        {
        }

        /// <summary>
        /// Use this to specify intervals directly
        /// </summary>
        /// <param name="_h"></param>
        /// <param name="_s"></param>
        /// <param name="_b"></param>
        public HSBFilter(FloatInterval _h, FloatInterval _s, FloatInterval _b)
        {
            m_HInterval = _h;
            m_SInterval = _s;
            m_BInterval = _b;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_hsbFilter"></param>
        internal HSBFilter(HSBFilter _hsbFilter)
        {
            this.m_HInterval = _hsbFilter.m_HInterval;
            this.m_SInterval = _hsbFilter.m_SInterval;
            this.m_BInterval = _hsbFilter.m_BInterval;
        }

        /// <summary>
        /// Gets or sets H
        /// </summary>
        public FloatInterval H
        {
            get { return m_HInterval; }
            set { m_HInterval = value; }
        }

        /// <summary>
        /// Gets or sets H
        /// </summary>
        public FloatInterval S
        {
            get { return m_SInterval; }
            set { m_SInterval = value; }
        }

        /// <summary>
        /// Gets or sets H
        /// </summary>
        public FloatInterval B
        {
            get { return m_BInterval; }
            set { m_BInterval = value; }
        }


        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            HSB hsb = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    hsb = new HSB(bitmap.GetPixel(x,y));

                    if (m_HInterval.Contains(hsb.H)
                        && m_SInterval.Contains(hsb.S)
                        && m_BInterval.Contains(hsb.B))
                    {
                        bitmap.SetPixel(x, y, hsb.ToColor());

                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new HSBFilter(this);
        }
    }
}
